package com.example.mythealthapp

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_home_menu.*
import kotlinx.android.synthetic.main.activity_more.*
import org.json.JSONObject
import java.io.BufferedReader
import java.io.InputStreamReader
import java.net.URL
import kotlin.concurrent.thread


class MoreActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_more)
        thread {
            try {
                val url= URL("https://baike.baidu.com/item/%E5%90%88%E7%90%86%E8%86%B3%E9%A3%9F/1817464")
                val urlConnection=url.openConnection()
                val inputStream=urlConnection.getInputStream()//字节
                val reader= InputStreamReader(inputStream)//字符
                val bufferedReader= BufferedReader(reader)//缓存
                var res=""
                var temp:String
                while ((bufferedReader.readLine())!=null){
                    temp=bufferedReader.readLine()
                    res+=temp
                }
                Log.i("MainActivity",res)

                if (res != null && res.startsWith("\ufeff"))
                    res = res.substring(1)

                Log.i("MainActivity",res)




                runOnUiThread {
                    val jsonObject = JSONObject(res)
                    val s: String = jsonObject.getString("content")
                    more_text.text = s
                }
                inputStream.close()
                reader.close()
                bufferedReader.close()
            }catch (e:Exception){
                e.printStackTrace()
            }
        }
    }
}